package com.itqf.controller;

import com.itqf.entity.ListUsers;
import com.itqf.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/13
 * @Time: 下午2:57
 */
@Controller
public class MyController {


    // 用户输入http://localhost:8080/hello  就会执行该方法
    //映射前端请求
    @RequestMapping(value = "/hello") //类似于  url-patten
    public  String  first(HttpServletRequest request){
        System.out.println("hello,我的第一个springmvc的案例！！！");
        request.setAttribute("name","springmvc中存储到request域中的数据！");
       // return  "/jsp/show.jsp";//跳转页面  物理视图
        return  "show";//跳转到  逻辑视图
        //物理视图 = 前缀(/jsp/)+逻辑视图(show)+后缀(.jsp) = /jsp/show.jsp
        //
    }

    @RequestMapping(value = "/hello1") //类似于  url-patten
    public  String  first1(HttpServletRequest request){
        System.out.println("hello,我的第一个springmvc的案例！！！------"+request.getParameter("name"));
        request.setAttribute("name","springmvc中存储到request域中的数据111！");
        return  "show1";//跳转页面
    }

    /**
     * 接收前端传递到服务端的参数
     * @param name
     * @param pwd
     * @return
     */
    @RequestMapping("/login")//
    public   String   login(String name,String pwd){
        System.out.println("name="+name+"---pwd="+pwd);
        return "show";
    }

    //四五个参数

    /**
     * 多参数时直接封装到对象中
     * @param users
     * @return
     */
    @RequestMapping("/register")
    public   String   regist(Users users,HttpServletRequest request){
        System.out.println(users);
        request.setAttribute("name",users.toString());
        return "show";
    }

    /**
     *集合
     * @param listUsers
     * @return
     */
    @RequestMapping("/register1")
    public   String   regist1(ListUsers listUsers, HttpServletRequest request){
        System.out.println(listUsers);
        request.setAttribute("name",listUsers.toString());
        return "show";
    }

    //  delete?id=10
    @RequestMapping("/delete")
    public  String  delete1(int id){
        System.out.println("传统：id="+id);
        return  "show";
    }

    //   delete/10
    @RequestMapping("/delete/{userId}")
    //@PathVariable("userId")   获得地址栏的userId位置的变量的值，并且赋值给参数
    public  String  delete2(@PathVariable("userId")  int id){
        System.out.println("REST：id="+id);
        return  "show";
    }


    //   update/12/张三
    @RequestMapping("/update/{userId}/{name}")
    //@PathVariable("userId")   获得地址栏的userId位置的变量的值，并且赋值给参数
    public  String  update(@PathVariable("userId")  int id,@PathVariable String name){
        System.out.println("REST：id="+id+"--name"+name);
        return  "show";
    }


    @RequestMapping("/test1")
    public  String   test1(HttpServletRequest request){
        //request.getRequestDispatcher("/jsp/show.jsp").forward();
        System.out.println("test1......");

        request.setAttribute("name","转发forward");
        //跳页面 转发
        return "forward:/jsp/show.jsp";//转发重定向不走视图解析器
    }

    @RequestMapping("/test2")
    public  String   test2(HttpServletRequest request){
        request.setAttribute("name","重定向redirect"); //请求域数据不能共享
        //跳页面  重定向
        return "redirect:/jsp/show.jsp";//转发重定向不走视图解析器
    }

    @RequestMapping("/test3")
    public  String   test3(HttpServletRequest request){
        System.out.println("test3......");
        //另一个请求(/test1)   转发
        return "forward:/test1";//
    }



}
